package com.allen.app.common.config.other;

import com.allen.app.common.utils.CheckObjectUtils;
import com.allen.app.common.utils.Try;
import java.util.StringJoiner;
import lombok.Getter;
import lombok.Setter;

/**
*
* Description: 业务异常对象,全局捕获此异常信息并发送到钉钉
* 继承RuntimeException 异常,具备事务回滚能力
*
* @author Limiaojun
* @date 2019/3/31 13:20
**/
public class BusinessException extends RuntimeException {

    /**
     * 附加返回
     */
    @Getter
    @Setter
    private Object additionResult;

    /**
     * 附加参数
     */
    @Getter
    @Setter
    private Object additionParam;

    /**
     * 是否发送钉钉消息 默认true
     */
    @Getter
    private boolean isSendDingTalk = true;

    /**
     * 是否输出异常信息到控制台  默认true
     */
    @Getter
    private boolean isPrintLog = true;

    public BusinessException() {
        super();
    }

    public BusinessException(String message) {
        super(message);
    }

    /**
     * This is a BusinessException.
     * @param message  异常信息
     * @param additionResult  附加返回
     * @return com.leon.common.config.BusinessException
     * @author Limiaojun
     * @date 2020/3/7 0007 10:37
     */
    public BusinessException(String message,Object additionResult) {
        super(message);
        this.additionResult = additionResult;
    }

    /**
     * This is a BusinessException.
     * @param message  异常信息
     * @param additionResult  附加返回
     * @param additionParam  附加参数
     * @return com.leon.common.config.BusinessException
     * @author Limiaojun
     * @date 2020/3/7 0007 10:37
     */
    public BusinessException(String message,Object additionResult ,Object additionParam) {
        super(message);
        this.additionResult = additionResult;
        this.additionParam = additionParam;
    }

    /**
     * This is a BusinessException.
     * @param message  异常信息
     * @param additionParam  附加参数
     * @param cause  异常对象
     * @return com.leon.common.config.BusinessException
     * @author Limiaojun
     * @date 2020/3/7 0007 10:37
     */
    public BusinessException(String message, Object additionParam , Throwable cause) {
        super(message,cause);
        this.additionParam = additionParam;
    }

    public BusinessException(String message, Throwable cause) {
        super(message, cause);
    }

    public BusinessException(Throwable cause) {
        super(cause);
    }

    public BusinessException(String message, Throwable cause, boolean enableSuppression,
        boolean writableStackTrace) {
        super(message, cause, enableSuppression, writableStackTrace);
    }

    /**
    * 设置异常发送钉钉开关,默认开启
    * @param sendDingTalk  是否开启 默认true
    * @return com.leon.common.config.BusinessException
    * @author Limiaojun
    * @date 2020/1/9 12:24
    */
    public BusinessException isSendDingTalk(boolean sendDingTalk) {
        this.isSendDingTalk = sendDingTalk;
        return this;
    }

    /**
     * 设置是否输出日志.
     * @param flag 设置是否输出日志,默认true
     * @return com.lhiot.minapp.config.BusinessException
     * @author Limiaojun
     * @date 2019/8/22 11:45
     */
    public BusinessException isPrintLog(boolean flag){
        this.isPrintLog = flag;
        return this;
    }

    @Override
    public String toString() {
        StringJoiner sJoiner = new StringJoiner("", "{", "}");
        sJoiner.add("message=").add(getMessage())
        .add(", additionResult=").add(String.valueOf(additionResult))
        .add(", additionParam=").add(String.valueOf(additionParam));

        String exceptionStr = Try.tryCatch(() -> {
            return super.getStackTrace()[0].toString();
        }, null);
        if(CheckObjectUtils.nonNull(exceptionStr)){
            sJoiner.add(", exception=").add(exceptionStr);
        }
        return sJoiner.toString();
    }
}